package cn.nfwx.tv.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.nfwx.tv.R;

/**
 * Created by dell on 2017/11/14.
 */

public class ItemHotColumn extends LinearLayout {

    private String labelStr;
    private String contentStr;
    private TextView label;
    private TextView content;

    public ItemHotColumn(Context context) {
        this(context, null);
    }

    public ItemHotColumn(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ItemHotColumn(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.ItemHotColumn);
        if (t.hasValue(R.styleable.ItemHotColumn_label)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                if (t.getType(R.styleable.ItemHotColumn_label) == TypedValue.TYPE_STRING) {
                    labelStr = t.getString(R.styleable.ItemHotColumn_label);
                } else {
                    int rid = t.getResourceId(R.styleable.ItemHotColumn_label, -1);
                    if (rid != -1) {
                        labelStr = context.getString(rid);
                    }
                }
            } else {
                labelStr = t.getString(R.styleable.ItemHotColumn_label);
                int rid = t.getResourceId(R.styleable.ItemHotColumn_label, -1);
                if (rid != -1) {
                    labelStr = context.getString(rid);
                }
            }
        }
        if (t.hasValue(R.styleable.ItemHotColumn_text)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                if (t.getType(R.styleable.ItemHotColumn_text) == TypedValue.TYPE_STRING) {
                    contentStr = t.getString(R.styleable.ItemHotColumn_text);
                } else {
                    int rid = t.getResourceId(R.styleable.ItemHotColumn_text, -1);
                    if (rid != -1) {
                        contentStr = context.getString(rid);
                    }
                }
            } else {
                contentStr = t.getString(R.styleable.ItemHotColumn_text);
                int rid = t.getResourceId(R.styleable.ItemHotColumn_text, -1);
                if (rid != -1) {
                    contentStr = context.getString(rid);
                }
            }
        }
        t.recycle();
        init();
    }

    private void init() {
        setOrientation(LinearLayout.HORIZONTAL);
        setGravity(Gravity.CENTER_VERTICAL);

        label = new TextView(getContext());
        label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        label.setTextColor(Color.parseColor("#222222"));
        label.setTextSize(13);

        if (!TextUtils.isEmpty(labelStr)) {
            StringBuilder stringBuilder = new StringBuilder(labelStr);
            stringBuilder.append("：");
            label.setText(stringBuilder);
        }

        addView(label);

        content = new TextView(getContext());
        content.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        content.setTextColor(Color.parseColor("#222222"));
        content.setTextSize(13);

        if (!TextUtils.isEmpty(contentStr)) {
            content.setText(contentStr);
        }

        addView(content);
    }

    /**
     * 设置标题
     *
     * @param labelStr
     */
    public void setLabel(String labelStr) {
        if (!TextUtils.isEmpty(labelStr)) {
            this.labelStr = labelStr;
            StringBuilder stringBuilder = new StringBuilder(labelStr);
            stringBuilder.append("：");
            label.setText(stringBuilder);
        }
    }

    /**
     * 设置内容
     *
     * @param contentStr
     */
    public void setText(String contentStr) {
        if (!TextUtils.isEmpty(contentStr)) {
            this.contentStr = contentStr;
            content.setText(contentStr);
        }
    }
}
